//
//  UILabel+Attributed.swift
//  HiggsBio
//
//  Created by Yun on 2017/5/10.
//  Copyright © 2017年 Higgs. All rights reserved.
//

import UIKit
import Foundation

extension UILabel{
    
    
    /// 设置单个高亮字
    ///
    /// - Parameters:
    ///   - oriText: 原始文字
    ///   - oriColor: 原始颜色
    ///   - oriFont: 原始字体
    ///   - attStr: 高亮文字
    ///   - attColor:高亮颜色
    ///   - attFont: 高亮字体
    ///   - lineSpac: 行间距
    ///   - chatSpac: 字间距
    ///   - alignment: 字体对齐方式
    ///   - lineBreakMode: linebreakmode
    public func setSignAttributedString(oriText:String,
                                 oriColor:UIColor,
                                 oriFont:UIFont,
                                 attStr:String,
                                 attColor:UIColor,
                                 attFont:UIFont,
                                 lineSpac:Float,
                                 chatSpac:Float,
                                 alignment:NSTextAlignment,
                                 lineBreakMode:NSLineBreakMode) {
        self.setAttributedString(oriText: oriText,
                                 oriColor: oriColor,
                                 oriFont: oriFont,
                                 attArray: [["string":attStr,"color":attColor,"font":attFont]],
                                 lineSpac: lineSpac,
                                 chatSpac: chatSpac,
                                 alignment: alignment,
                                 lineBreakMode: lineBreakMode)
    }
    
    /// 设置label的富文本
    ///
    /// - Parameters:
    ///   - oriText: 原始要显示的文字
    ///   - oriColor: 原始颜色
    ///   - oriFont: 原始字体，数组嵌套字典（string，font，color）
    ///   - attArray: 高亮的元素
    ///   - lineSpac: 行间距
    ///   - chatSpac: 字间距
    ///   - alignment: 对齐方式
    ///   - lineBreakMode: linebreakmode
    public func setAttributedString(oriText:String,
                             oriColor:UIColor,
                             oriFont:UIFont,
                             attArray:NSArray,
                             lineSpac:Float,
                             chatSpac:Float,
                             alignment:NSTextAlignment,
                             lineBreakMode:NSLineBreakMode) {
        let mutaString = NSMutableAttributedString(string: oriText)
        let nsStr:NSString = oriText as NSString
        let lastRange = NSMakeRange(0, nsStr.length)
    
        mutaString.addAttributes([NSAttributedString.Key.foregroundColor:oriColor], range: lastRange)//原始颜色
        mutaString.addAttributes([NSAttributedString.Key.font:oriFont], range: lastRange)//原始字体
        if chatSpac != 0{
            mutaString.addAttributes([NSAttributedString.Key.kern:NSNumber(value: chatSpac)], range: lastRange)//字间距
        }
        let paragraphStyle = NSMutableParagraphStyle()
        if lineSpac != 0{
            paragraphStyle.lineSpacing = CGFloat(lineSpac)//设置行间距
        }
        paragraphStyle.alignment = alignment//对齐方式
        paragraphStyle.lineBreakMode = lineBreakMode
        mutaString.addAttributes([NSAttributedString.Key.paragraphStyle:paragraphStyle], range: lastRange)
        //循环编辑其他高亮元素
        if(attArray.count>0){
            for i in 0...(attArray.count-1){
                let dic:NSDictionary = attArray[i] as! NSDictionary
                let attStr:NSString = dic["string"] as! NSString
                if(attStr.length>0){
                    let attFont:UIFont = dic["font"] as! UIFont
                    let attColor:UIColor = dic["color"] as! UIColor
                    let attrange = self.rangesOfString(searchString: attStr, str: oriText as NSString)
                    for range in attrange{
                        mutaString.addAttributes([NSAttributedString.Key.font:attFont], range: range as! NSRange)
                        mutaString.addAttributes([NSAttributedString.Key.foregroundColor:attColor], range: range as! NSRange)
                    }
                }
            }
        }
        
        
        self.attributedText = mutaString
    }
    
    public func rangesOfString(searchString:NSString,str:NSString) -> NSArray{
        let results:NSMutableArray = NSMutableArray()
        var searchRange = NSMakeRange(0, str.length)
        var range = str.range(of: searchString as String, options: .caseInsensitive, range: searchRange)
        
        while (range.location != NSNotFound) {
            results.add(range)
            searchRange = NSMakeRange(NSMaxRange(range), str.length - NSMaxRange(range))
            range = str.range(of: searchString as String, options: .caseInsensitive, range: searchRange)
        }
        return results.copy() as! NSArray
    }
    
    /// 设置
    ///
    /// - Parameters:
    ///   - oriText: 设置显示文字
    ///   - lineSpac: 行间距
    ///   - chatSpac: 字间距
    ///   - alignment: 对齐方式
    ///   - lineBreakMode:
    public func setSpac(oriText:String,lineSpac:Float = 5,chatSpac:Float = 0,alignment:NSTextAlignment = .left,lineBreakMode:NSLineBreakMode = .byTruncatingTail) {
        let mutaString = NSMutableAttributedString(string: oriText)
        let nsStr:NSString = oriText as NSString
        let lastRange = NSMakeRange(0, nsStr.length)
        if chatSpac != 0{
            mutaString.addAttributes([NSAttributedString.Key.kern:NSNumber(value: chatSpac)], range: lastRange)//字间距
        }
        let paragraphStyle = NSMutableParagraphStyle()
        if lineSpac != 0{
            paragraphStyle.lineSpacing = CGFloat(lineSpac)//设置行间距
        }
        paragraphStyle.alignment = alignment//对齐方式
        paragraphStyle.lineBreakMode = lineBreakMode
        mutaString.addAttributes([NSAttributedString.Key.paragraphStyle:paragraphStyle], range: lastRange)
        self.attributedText = mutaString
    }
    
    
    /// 设置左右对齐
    ///
    /// - Parameters:
    ///   - title: 文字
    ///   - labelWidth: label的宽度
    ///   - labelHeight: label的高度
    ///   - textColor: label字体颜色
    ///   - font: 字体
    public func setLeftRightAligin(title:String,labelWidth:CGFloat,labelHeight:CGFloat,font:UIFont, textColor:UIColor) {
        let muAttrString:NSMutableAttributedString = NSMutableAttributedString(string: title)
        let width = title.widthWithConstrainedWidth(height: labelHeight, font: font)
        let num:NSNumber = NSNumber(value: Float(labelWidth - width)/Float(title.count - 1))
        muAttrString.addAttribute(NSAttributedString.Key.kern, value: num, range: NSMakeRange(0, title.count-1))
        muAttrString.addAttribute(NSAttributedString.Key.font, value: font.fontName, range: NSMakeRange(0, title.count))
        muAttrString.addAttribute(NSAttributedString.Key.font, value: font, range: NSMakeRange(0, title.count))
        muAttrString.addAttribute(NSAttributedString.Key.foregroundColor, value: textColor, range: NSMakeRange(0, title.count))
        self.attributedText = muAttrString
    }
}
